﻿/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "tet_mesh.h"

void gctl::tetrahedron_mesh::init(std::string in_name, std::string in_info, const array<vertex3dc> &in_nodes, 
	const array<tetrahedron> &in_tets)
{
	if (initialized)
	{
		std::string err_str = "The object is already initialized. From tetrahedron_mesh::init(...)";
		throw runtime_error(err_str);
	}

	base_mesh::init(TRI_TET_MESH, MESH_3D, in_name, in_info);

	node_num = in_nodes.size();
	ele_num  = in_tets.size();

	nodes.resize(node_num);
	for (int i = 0; i < node_num; i++)
	{
		nodes[i] = in_nodes[i];
	}

	elements.resize(ele_num);
	for (int i = 0; i < ele_num; i++)
	{
		elements[i].id = i;
		for (int j = 0; j < 4; j++)
		{
			elements[i].vert[j] = nodes.get(in_tets[i].vert[j]->id);
		}
		elements[i].deter_vert_order();
	}

	initialized = true;
	return;
}

void gctl::tetrahedron_mesh::load_binary(std::string filename)
{
	if (initialized)
	{
		std::string err_str = "The object is already initialized. From tetrahedron_mesh::load_binary(...)";
		throw runtime_error(err_str);
	}

	std::ifstream infile;
	gctl::open_infile(infile, filename, ".2m", 
		std::ios::in|std::ios::binary);

	// 读入网格头信息
	load_headinfo(infile, TRI_TET_MESH, MESH_2D);

	// 读入网格信息
	infile.read((char*)&node_num, sizeof(int));
	infile.read((char*)&ele_num, sizeof(int));

	nodes.resize(node_num);
	elements.resize(ele_num);

	for (int i = 0; i < node_num; i++)
	{
		infile.read((char*)nodes.get(i), sizeof(gctl::vertex3dc));
	}

	int in_index;
	for (int i = 0; i < ele_num; i++)
	{
		elements[i].id = i;
		for (int j = 0; j < 4; j++)
		{
			infile.read((char*)&in_index, sizeof(int));
			elements[i].vert[j] = nodes.get(in_index);
		}
		elements[i].deter_vert_order();
	}
	initialized = true;

	// 读入模型数据单元
	load_datablock(infile);

	infile.close();
	return;
}

void gctl::tetrahedron_mesh::save_binary(std::string filename)
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From tetrahedron_mesh::save_binary(...)";
		throw runtime_error(err_str);
	}

	std::ofstream outfile;
	gctl::open_outfile(outfile, filename, ".2m", 
		std::ios::out|std::ios::binary);

	// 首先输出网格的头信息
	save_headinfo(outfile);

	// 输出网格信息
	outfile.write((char*)&node_num, sizeof(int));
	outfile.write((char*)&ele_num, sizeof(int));

	for (int i = 0; i < node_num; i++)
	{
		outfile.write((char*)nodes.get(i), sizeof(gctl::vertex3dc));
	}

	int in_index;
	for (int i = 0; i < ele_num; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			in_index = elements[i].vert[j]->id;
			outfile.write((char*)&in_index, sizeof(int));
		}
	}

	// 输出的模型数据单元
	save_datablock(outfile);

	outfile.close();
	return;
}

void *gctl::tetrahedron_mesh::get_node(unsigned int idx)
{
	std::string err_str;
	if (!initialized)
	{
		err_str = "The object is not initialized. From gctl::tetrahedron_mesh::get_node(...)";
		throw runtime_error(err_str);
	}

	if (idx >= node_num)
	{
		err_str = "Invalid index. From gctl::tetrahedron_mesh::get_node(...)";
		throw runtime_error(err_str);
	}

	return nodes.get(idx);
}

void *gctl::tetrahedron_mesh::get_element(unsigned int idx)
{
	std::string err_str;
	if (!initialized)
	{
		err_str = "The object is not initialized. From gctl::tetrahedron_mesh::get_element(...)";
		throw runtime_error(err_str);
	}

	if (idx >= ele_num)
	{
		err_str = "Invalid index. From gctl::tetrahedron_mesh::get_element(...)";
		throw runtime_error(err_str);
	}

	return elements.get(idx);
}

void *gctl::tetrahedron_mesh::fget_node(unsigned int idx)
{
	return nodes.get(idx);
}

void *gctl::tetrahedron_mesh::fget_element(unsigned int idx)
{
	return elements.get(idx);
}

gctl::tetrahedron_mesh::tetrahedron_mesh() : base_mesh::base_mesh(){}

gctl::tetrahedron_mesh::tetrahedron_mesh(std::string in_name, std::string in_info, 
	const array<vertex3dc> &in_nodes, const array<tetrahedron> &in_tets)
{
	init(in_name, in_info, in_nodes, in_tets);
}

gctl::tetrahedron_mesh::~tetrahedron_mesh(){}

const gctl::array<gctl::vertex3dc> *gctl::tetrahedron_mesh::get_nodes() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From tetrahedron_mesh::get_nodes(...)";
		throw runtime_error(err_str);
	}

	return &nodes;
}

const gctl::array<gctl::tetrahedron> *gctl::tetrahedron_mesh::get_elements() const
{
	if (!initialized)
	{
		std::string err_str = "The object is not initialized. From tetrahedron_mesh::get_elements(...)";
		throw runtime_error(err_str);
	}

	return &elements;
}

void gctl::tetrahedron_mesh::load_tetgen(std::string filename, index_packed_e packed)
{
	gctl::read_Tetgen_node(filename, nodes, packed);
	gctl::read_Tetgen_element(filename, elements, nodes, packed);

	// 设置名称与信息等
	node_num = nodes.size();
	ele_num  = elements.size();
	base_mesh::init(TRI_TET_MESH, MESH_3D, filename, "Imported from a .node and .ele file.");
	initialized = true;

	return;
}

void gctl::tetrahedron_mesh::load_gmsh(std::string filename, index_packed_e packed)
{
	std::ifstream infile;
	gctl::open_infile(infile, filename, ".msh");
	gctl::read_gmsh_node(infile, nodes, packed);
	gctl::read_gmsh_element(infile, elements, nodes, packed);
	infile.close();

	// 设置名称与信息等
	node_num = nodes.size();
	ele_num  = elements.size();
	base_mesh::init(TRI_TET_MESH, MESH_3D, filename, "Imported from a .msh file.");
	initialized = true;

	return;
}

void gctl::tetrahedron_mesh::save_gmsh(std::string filename, index_packed_e packed)
{
	std::ofstream outfile;
	gctl::open_outfile(outfile, filename, ".msh");
	gctl::save2gmsh(outfile, elements, nodes, packed);
	outfile.close();
	return;
}

void gctl::tetrahedron_mesh::save_gmsh(std::string filename, mesh_data_type_e d_type, output_type_e out_mode, index_packed_e packed)
{
	base_mesh::save_gmsh(filename, d_type, out_mode, packed);
	return;
}

void gctl::tetrahedron_mesh::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed)
{
	base_mesh::save_gmsh(filename, datname, out_mode, packed);
	return;
}
